package com.ss.warfarin.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ss.warfarin.persistence.model.Patient;
import com.ss.warfarin.service.ProfileService;

/**
 * Servlet implementation class PatientController
 */
public class ProfileController extends HttpServlet {
	
	private static final long serialVersionUID = -6621840395994866857L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String patientId = request.getParameter("patientProfileId");
		
		ProfileService profileService = new ProfileService();
		Patient patient = profileService.searchPatientProfileId(patientId);

		if (patient != null) {
			
			Map<String, Integer> age = profileService.calculateAge(patient);
			
			request.setAttribute("patient", patient);
			request.setAttribute("age", age);
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsp/history.jsp");
			requestDispatcher.forward(request, response);
		}
	}

}
